/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.sciTechnologyDemand.service;

import com.fzhucc.modules.sciTechnologyDemand.domain.SciTechnologyDemandInfo;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.sciTechnologyDemand.repository.SciTechnologyDemandInfoRepository;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.SciTechnologyDemandInfoDto;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.SciTechnologyDemandInfoQueryCriteria;
import com.fzhucc.modules.sciTechnologyDemand.service.mapstruct.SciTechnologyDemandInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-01-03
**/
@Service
@RequiredArgsConstructor
public class SciTechnologyDemandInfoService {

    private final SciTechnologyDemandInfoRepository sciTechnologyDemandInfoRepository;
    private final SciTechnologyDemandInfoMapper sciTechnologyDemandInfoMapper;

    public Map<String,Object> queryAll(SciTechnologyDemandInfoQueryCriteria criteria, Pageable pageable){
        Page<SciTechnologyDemandInfo> page = sciTechnologyDemandInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sciTechnologyDemandInfoMapper::toDto));
    }

    public List<SciTechnologyDemandInfoDto> queryAll(SciTechnologyDemandInfoQueryCriteria criteria){
        return sciTechnologyDemandInfoMapper.toDto(sciTechnologyDemandInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public SciTechnologyDemandInfoDto findById(Integer id) {
        SciTechnologyDemandInfo sciTechnologyDemandInfo = sciTechnologyDemandInfoRepository.findById(id).orElseGet(SciTechnologyDemandInfo::new);
        ValidationUtil.isNull(sciTechnologyDemandInfo.getId(),"SciTechnologyDemandInfo","id",id);
        return sciTechnologyDemandInfoMapper.toDto(sciTechnologyDemandInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    public SciTechnologyDemandInfoDto create(SciTechnologyDemandInfo resources) {
        return sciTechnologyDemandInfoMapper.toDto(sciTechnologyDemandInfoRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(SciTechnologyDemandInfo resources) {
        SciTechnologyDemandInfo sciTechnologyDemandInfo = sciTechnologyDemandInfoRepository.findById(resources.getId()).orElseGet(SciTechnologyDemandInfo::new);
        ValidationUtil.isNull( sciTechnologyDemandInfo.getId(),"SciTechnologyDemandInfo","id",resources.getId());
        sciTechnologyDemandInfo.copy(resources);
        sciTechnologyDemandInfoRepository.save(sciTechnologyDemandInfo);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            sciTechnologyDemandInfoRepository.deleteById(id);
        }
    }

    public void download(List<SciTechnologyDemandInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SciTechnologyDemandInfoDto sciTechnologyDemandInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("tb_sci_technology_demand id", sciTechnologyDemandInfo.getTdId());
            map.put("本企业劳模、工匠情况姓名", sciTechnologyDemandInfo.getFirstFinisherName());
            map.put("本企业劳模、工匠情况职务", sciTechnologyDemandInfo.getUnitPosition());
            map.put("本企业劳模、工匠情况劳模工匠人才类型zqx_ma_type", sciTechnologyDemandInfo.getMaType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}